package samples.chapter10;

import lombok.Data;
import lombok.val;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class Chapter10User implements UserDetails {
    private Integer id;
    private String username;
    private String password;
    private Boolean locked;
    private List<Chapter10Role> roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        val authorities = new ArrayList<GrantedAuthority>();
        for (Chapter10Role role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
